/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package deatax.display;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author nrechten
 */
public class ProximityGroup {
    private List<ProximityTween> group = new ArrayList<ProximityTween>();

    public void calcSubGroups(){
        for (ProximityTween pt1 : group) {
            pt1.setTouching(null);
            pt1.resetLinks();
            pt1.clearDirect();
            for (ProximityTween pt2 : group) {
                if(pt1.check(pt2)){
                    pt1.incLinks();
                    pt1.addDirect(pt2);
                    if(pt1.getTouching() != null && pt2.getTouching() != null){
                        pt1.getTouching().merge(pt2.getTouching());
                    } else if(pt1.getTouching() != null) {
                        pt2.setTouching(pt1.getTouching());
                    } else if(pt2.getTouching() != null){
                        pt1.setTouching(pt2.getTouching());
                    } else {
                        pt1.setTouching(new ProximityGroup());
                        pt2.setTouching(pt1.getTouching());
                    }
                }
            }
        }
    }

    public void merge(ProximityGroup pg){
        if(pg == this) return;
        for (ProximityTween proximityTween : pg.group) {
            proximityTween.setTouching(this);
        }
        pg.group.clear();
    }

    public void remove(ProximityTween t){
        group.remove(t);
    }

    public void add(ProximityTween t){
        group.add(t);
    }
}
